package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.DailyTrain;
import com.ruoyi.business.domain.dto.DailyTrainStationGroupDTO;
import com.ruoyi.business.mapper.DailyTrainMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DailyTrainStationMapper;
import com.ruoyi.business.domain.DailyTrainStation;
import com.ruoyi.business.service.IDailyTrainStationService;

/**
 * 每日车站Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class DailyTrainStationServiceImpl implements IDailyTrainStationService 
{
    @Autowired
    private DailyTrainStationMapper dailyTrainStationMapper;
    @Autowired
    private DailyTrainMapper dailyTrainMapper;

    /**
     * 查询每日车站
     * 
     * @param id 每日车站主键
     * @return 每日车站
     */
    @Override
    public DailyTrainStation selectDailyTrainStationById(Long id)
    {
        return dailyTrainStationMapper.selectDailyTrainStationById(id);
    }

    /**
     * 查询每日车站列表
     * 
     * @param dailyTrainStation 每日车站
     * @return 每日车站
     */
    @Override
    public List<DailyTrainStation> selectDailyTrainStationList(DailyTrainStation dailyTrainStation)
    {
        return dailyTrainStationMapper.selectDailyTrainStationList(dailyTrainStation);
    }

    /**
     * 新增每日车站
     * 
     * @param dailyTrainStation 每日车站
     * @return 结果
     */
    @Override
    public int insertDailyTrainStation(DailyTrainStation dailyTrainStation)
    {
        dailyTrainStation.setCreateTime(DateUtils.getNowDate());
        return dailyTrainStationMapper.insertDailyTrainStation(dailyTrainStation);
    }

    /**
     * 修改每日车站
     * 
     * @param dailyTrainStation 每日车站
     * @return 结果
     */
    @Override
    public int updateDailyTrainStation(DailyTrainStation dailyTrainStation)
    {
        dailyTrainStation.setUpdateTime(DateUtils.getNowDate());
        return dailyTrainStationMapper.updateDailyTrainStation(dailyTrainStation);
    }

    /**
     * 批量删除每日车站
     * 
     * @param ids 需要删除的每日车站主键
     * @return 结果
     */
    @Override
    public int deleteDailyTrainStationByIds(Long[] ids)
    {
        return dailyTrainStationMapper.deleteDailyTrainStationByIds(ids);
    }

    /**
     * 删除每日车站信息
     * 
     * @param id 每日车站主键
     * @return 结果
     */
    @Override
    public int deleteDailyTrainStationById(Long id)
    {
        return dailyTrainStationMapper.deleteDailyTrainStationById(id);
    }

    @Override
    public List<DailyTrainStationGroupDTO> selectDailyTrainStationGroupByTrainCode(DailyTrainStation dailyTrainStation) {
        // 获取所有符合条件的车站数据(不分页)
        List<DailyTrainStation> allStations = dailyTrainStationMapper.selectDailyTrainStationList(dailyTrainStation);

        // 按trainCode分组
        Map<String, List<DailyTrainStation>> stationGroups = allStations.stream()
                .collect(Collectors.groupingBy(DailyTrainStation::getTrainCode));

        // 转换为DTO对象
        List<DailyTrainStationGroupDTO> result = new ArrayList<>();

        for (Map.Entry<String, List<DailyTrainStation>> entry : stationGroups.entrySet()) {
            String trainCode = entry.getKey();
            List<DailyTrainStation> stations = entry.getValue();

            DailyTrainStationGroupDTO groupDTO = new DailyTrainStationGroupDTO();
            groupDTO.setTrainCode(trainCode);
            groupDTO.setStations(stations);

            // 获取车次类型
            if (stations.size() > 0) {
                DailyTrainStation firstStation = stations.get(0);
                DailyTrain train = new DailyTrain();
                train.setDate(firstStation.getDate());
                train.setCode(trainCode);
                List<DailyTrain> trains = dailyTrainMapper.selectDailyTrainList(train);
                if (!trains.isEmpty()) {
                    groupDTO.setTrainType(trains.get(0).getType());
                } else {
                    // 如果找不到车次，默认设置为其他类型
                    String defaultType = trainCode.startsWith("G") ? "G" :
                            trainCode.startsWith("D") ? "D" : "OTHER";
                    groupDTO.setTrainType(defaultType);
                }
            }

            result.add(groupDTO);
        }

        return result;
    }

    @Override
    public List<DailyTrainStation> selectByTrainCode(String date, String trainCode) {
        System.out.println(date+"DADADADADADAD");
        System.out.println(trainCode+"RTRTRTRTRTRTRTRT");
        return dailyTrainStationMapper.selectList(date, trainCode);
    }

    @Override
    public List<String> selectDistinctTrainCodes(DailyTrainStation dailyTrainStation, Integer pageNum, Integer pageSize) {
        // 计算偏移量
        int offset = (pageNum - 1) * pageSize;
        
        // 查询分页后的不重复车次编号
        return dailyTrainStationMapper.selectDistinctTrainCodes(dailyTrainStation, pageSize, offset);
    }

    @Override
    public int countDistinctTrainCodes(DailyTrainStation dailyTrainStation) {
        // 统计不重复车次编号的数量
        return dailyTrainStationMapper.countDistinctTrainCodes(dailyTrainStation);
    }

    @Override
    public List<DailyTrainStation> selectDailyTrainStationByTrainCodes(DailyTrainStation dailyTrainStation) {
        // 根据车次编号列表查询车站信息
        return dailyTrainStationMapper.selectDailyTrainStationByTrainCodes(dailyTrainStation);
    }
}
